 /* Exercise 6.23 Solution */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define MAX 100 /* the maximum number of commands */
/* function prototypes */
void getCommands( int commands[][ 2 ] );
int turnRight( int d );
int turnLeft( int d );
void movePen( int down, int a[][ 50 ], int dir, int dist );
void printArray( int a[][ 50 ] );
int main()
{
	int floor[ 50 ][ 50 ] = { 0 }; /* floor grid */
	int penDown = FALSE; /* pen down flag */
	int command;
	int direction = 0; /* direction indicator */
	int commandArray[ MAX ][ 2 ] = { 0 }; /* array of commands */
	int distance; /* distance to move */
	int count = 0; /* command counter */
	getCommands( commandArray );
	command = commandArray[ count ][ 0 ];
	 /* continue receiving input while -9 is not entered */
	 while ( command != 9 ) 
	 {
		/* determine what command was entered and perform action */
		 switch ( command ) 
		 {
			case 1:
				penDown = FALSE;
				break; /* exit switch */
			case 2:
				penDown = TRUE;
				break; /* exit switch*/
			case 3:
				direction = turnRight( direction );
				break; /* exit switch */
			case 4:
				direction = turnLeft( direction );
				break; /* exit switch */
			case 5:
				distance = commandArray[ count ][ 1 ];
				movePen( penDown, floor, direction, distance );
				break; /* exit switch */
			case 6:
				printf( "\nThe drawing is:\n\n" );
				printArray( floor );
				break; /* exit switch */
		} /* end switch */
		command = commandArray[ ++count ][ 0 ];
	} 
}
void getCommands( int commands[][ 2 ] )
{
   int i; /* counter */
   int tempCommand; /* temporary command holder */
   printf( "Enter command ( 9 to end input ): " );
   scanf( "%d", &tempCommand );
   /* recieve commands until -9 or 100 commands are entered */
   for ( i = 0; tempCommand != 9 && i < MAX; i++ ) 
   {
	   commands[ i ][ 0 ] = tempCommand;
	   /* ignore comma after 5 is entered */
	   if ( tempCommand == 5 ) 
	   {
		  scanf( ",%d", &commands[ i ][ 1 ] );
	   } /* end if */
		printf( "Enter command ( 9 to end input ): " );
		scanf( "%d", &tempCommand );
   } 
	commands[ i ][ 0 ] = 9; /* last command */
} /* end function getCommands */

 /* turnRight turns turtle to the right */
int turnRight( int d )
{
	return ++d > 3 ? 0 : d;
} /* end function turnRight */
 /* turnLeft turns turtle to the left */
int turnLeft( int d )
{
	return --d < 0 ? 3 : d;
}
void movePen( int down, int a[][ 50 ], int dir, int dist )
{
	int i;
	int j; /* loop counter */
	static int xPos = 0; /* x coordinate */
	static int yPos = 0; /* y coordinate */
	/* determine which way to move pen */
	switch ( dir ) 
	{

		 case 0: /* move to the right */
		 /* move dist spaces or until edge of floor */
			for ( j = 1; j <= dist && yPos + j < 50; j++ ) 
			{
			 /* draw 1 if pen is down */
				if ( down )
				{
					 a[ xPos ][ yPos + j ] = 1;
				} /* end if */
			} /* end for */
			yPos += j - 1;
			break; /* exit switch */
		case 1: /* move down */
			/* move dist spaces or until edge of floor */
			for ( i = 1; i <= dist && xPos + i < 50; i++ ) 
			{
			 /* draw 1 if pen is down */
				if ( down ) 
				{
					a[ xPos + i ][ yPos ] = 1;
				} /* end if */
			} /* end for */
			xPos += i - 1;
			break; /* exit switch */
		case 2: /* move to the left */
		 /* move dist spaces or until edge of floor */
			 for ( j = 1; j <= dist && yPos - j >= 0; j++ )
			 {
			 /* draw 1 if pen is down */
				 if ( down )
				 {
					a[ xPos ][ yPos - j ] = 1;
				 } 
			} /* end for */
			yPos -= j - 1;
			break; /* exit switch */
    	 case 3: /* move up */
			 /* move dist spaces or until edge of floor */
			 for ( i = 1; i <= dist && xPos - i >= 0; i++ )
			 {
				 /* draw 1 if pen is down */
				 if ( down ) 
				 {
					a[xPos - i ][ yPos ] = 1;
				 } /* end if */
			 } /* end for */
			 xPos -= i - 1;
			 break; /* exit switch */
	 } /* end switch */
 } 
/* printArray prints array drawing */
void printArray( int a[][ 50 ] )
{
	int i; /* counter */
	int j; /* counter */
	/* loop through array */
	for ( i = 0; i < 50; i++ ) 
	{
	 /* loop through array */
		for ( j = 0; j < 50; j++ ) 
		{
			putchar( a[ i ][ j ] ? '*' : ' ' );
		} /* end for */
		putchar( '\n' );
	} /* end for */
} 